﻿using LearningEnglish.LogicDSL;
using LearningEnglish.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Web;
using System.Web.Mvc;
using MyX.DSL;

namespace LearningEnglish.Controllers.Api
{
    public class LearnUserController : Controller
    {
        [AcceptExAjax]
        [NormalJson]
        public async Task<ActionResult> Login(string name, string password)
        {
            var command = new LearnUserLogin() { Name = name, Password = password, Channel = Token.Channel.Web };
            command.Send();

            var status = await command.Status();

            this.HandleStatus(status);

            return Json(new { token = command.Result().ToString() }, JsonRequestBehavior.AllowGet);

        }

        [AcceptExAjax]
        [NormalJson]
        public async Task<ActionResult> Register(string name, string password)
        {
            var command = new CreateLearnUser() { Name = name, Password = password };
            command.Send();

            var status = await command.Status();

            this.HandleStatus(status);

            return Json(new { user = command.Result().ToString() }, JsonRequestBehavior.AllowGet);
        }
    }
}